package com.fire.control.system.config.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class QuartzManager {
	
	@Autowired Scheduler scheduler;
	
	/**
	* @Title: execute
	* @Description: TODO(添加或修改任务)
	* @author wanbo
	* @date  2017年6月20日 下午11:43:17
	* @param jobName
	* @param jobGroupName
	* @param triggerName
	* @param triggerGroupName
	* @param jobClass
	* @param cron
	* @throws Exception
	*/
	public boolean execute(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class <? extends Job> jobClass, String cron) {
		boolean b = false;
		try {
			TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
			//获取触发器
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			if (trigger==null) {
				addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron);
			} else {
				modifyJobTime(scheduler, triggerKey, trigger, cron);
			}
			b = true;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return b;
	}
	
	public boolean execute(String jobName, Class <? extends Job> jobClass, String cron){
		removeJob(jobName);
		return execute(jobName, jobName, jobName, jobName, jobClass, cron);
	}
	
	
	/**
	* @Title: addJob
	* @Description: TODO(添加任务)
	* @author wanbo
	* @date  2017年6月20日 下午11:43:09
	* @param jobName
	* @param jobGroupName
	* @param triggerName
	* @param triggerGroupName
	* @param jobClass
	* @param cron
	* @throws Exception
	*/
	private void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class <? extends Job> jobClass, String cron) {
		try {  
			//任务设置
	        JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
	        jobDetail.getJobDataMap().put("job", jobName);
	        //触发器设置
	        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
	        //设置调度器
	        scheduler.scheduleJob(jobDetail, trigger);
		} catch (Exception e) {
			throw new RuntimeException(e);
        } 
	}
	
	/**
	* @Title: modifyJobTime
	* @Description: TODO(修改任务)
	* @author wanbo
	* @date  2017年6月20日 下午11:42:56
	* @param scheduler
	* @param triggerKey
	* @param trigger
	* @param cron
	* @throws Exception
	*/
	private void modifyJobTime(Scheduler scheduler,TriggerKey triggerKey,CronTrigger trigger,String cron) {
        try {
			if (!trigger.getCronExpression().equalsIgnoreCase(cron)) {
				//重置触发器
			    trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
			    //重置调度器
			    scheduler.rescheduleJob(triggerKey, trigger);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
        
	}

	
    /**
    * @Title: removeJob
    * @Description: TODO(移除一个任务)
    * @author wanbo
    * @date  2017年6月20日 下午11:40:31
    * @param jobName
    * @param jobGroupName
    * @param triggerName
    * @param triggerGroupName
    */
    public boolean removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {  
        boolean b = false;
    	try {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
            //停止触发器
            scheduler.pauseTrigger(triggerKey);
            //移除触发器
            scheduler.unscheduleJob(triggerKey);  
            //删除任务
            scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
            b = true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
    	return b;
    }
    
    public boolean removeJob(String jobName){
    	return removeJob(jobName, jobName, jobName, jobName);
    }
    
    
    /**
    * @Title: shutdownJobs
    * @Description: TODO(关闭所有定时任务 )
    * @author wanbo
    * @date  2017年6月20日 下午11:42:02
    */
    public boolean shutdownJobs() {
    	boolean b = false;
        try {
            if (!scheduler.isShutdown()) {  
            	scheduler.shutdown();  
            }  
            b = true;
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
        return b;
    }  
	
}
